<?php

namespace Admin\Controller;

use Common\Controller\AuthController; 
use OT\TagLib\Think;

class CatgoryController extends AuthController{

	public function goods_catgory()
	{
		$p = I('p')?I('p'):1;
		$count = M('goods_catgory')->count();
		$Page = new \Think\Page($count,20);
		$show = $Page->show();
		$catgory = M('goods_catgory')->page($p,20)->select();
		$this->assign('catgory',$catgory);
		$this->assign('page',$show);
		$this->display();
	}

	public function goods_del(){
		$id = I('id');
		if (empty($id) || !is_numeric($id)) {
			$this->error('参数不合法');
		}
		$res = M('goods_catgory')->where(['id'=>$id])->delete();
		if ($res) {
			$this->redirect('goods_catgory');
		}
	}

	public function edit_goods(){
		$id = I('id');
		if (empty($id) || !is_numeric($id)) {
			$this->error('参数不合法');
		}
		$exists = M('goods_catgory')->where(['id'=>$id])->find();
		if (!$exists) {
			$this->error('分类不存在');
		}
		$this->assign('catgory',$exists);
		$this->display();
	}

	public function edit_goods_run(){


		if (!IS_AJAX) {
			$this->error('提交方式不正确');
		}
		$exists = M('goods_catgory')->where(['name'=>$_POST['name']])->where('id!='.$_POST['id'])->find();
		if ($exists) {
			$this->error('商品分类名已存在');
		}
		$res = M('goods_catgory')->save($_POST);
		if ($res) {
			$this->success('修改成功',U('goods_catgory'),1);
		}

	}

	public function add_goods(){

		$this->display();

	}

	public function add_goods_run(){

		if (!IS_AJAX) {
			$this->error('提交方式不正确');
		}
		$exists = M('goods_catgory')->where(['name'=>$_POST['name']])->find();
		if ($exists) {
			$this->error('商品分类名已存在');
		}
		$res = M('goods_catgory')->add($_POST);
		if ($res) {
			$this->success('添加成功',U('goods_catgory'),1);
		}

	}

	public function country_catgory(){

		$p = I('p')?I('p'):1;
		$count = M('country_list')->count();
		$Page = new \Think\Page($count,20);
		$show = $Page->show();
		$catgory = M('country_list')->page($p,20)->select();
		$this->assign('catgory',$catgory);
		$this->assign('page',$show);
		$this->display();

	}

	public function country_del(){

		$id = I('id');
		if (empty($id) || !is_numeric($id)) {
			$this->error('参数不合法');
		}
		$res = M('country_list')->where(['id'=>$id])->delete();
		if ($res) {
			$this->redirect('country_catgory');
		}

	}

	public function edit_country(){

		$id = I('id');
		if (empty($id) || !is_numeric($id)) {
			$this->error('参数不合法');
		}
		$exists = M('country_list')->where(['id'=>$id])->find();
		if (!$exists) {
			$this->error('分类不存在');
		}
		$this->assign('catgory',$exists);
		$this->display();

	}

	public function edit_country_run(){

		if (!IS_AJAX) {
			$this->error('提交方式不正确');
		}
		$exists = M('country_list')->where(['name'=>$_POST['name']])->where('id!='.$_POST['id'])->find();
		if ($exists) {
			$this->error('分类名已存在');
		}
		$res = M('country_list')->save($_POST);
		if ($res) {
			$this->success('修改成功',U('country_catgory'),1);
		}

	}

	public function add_country(){

		$this->display();

	}

	public function add_country_run(){

		if (!IS_AJAX) {
			$this->error('提交方式不正确');
		}
		$exists = M('country_list')->where(['name'=>$_POST['name']])->find();
		if ($exists) {
			$this->error('国家地区名已存在');
		}
		$res = M('country_list')->add($_POST);
		if ($res) {
			$this->success('添加成功',U('country_catgory'),1);
		}

	}


	public function city_catgory(){

		$p = I('p')?I('p'):1;
		$count = M('city_list')->count();
		$Page = new \Think\Page($count,20);
		$show = $Page->show();
		$catgory = M('city_list')->page($p,20)->select();
		$this->assign('catgory',$catgory);
		$this->assign('page',$show);
		$this->display();

	}

	public function city_del(){

		$id = I('id');
		if (empty($id) || !is_numeric($id)) {
			$this->error('参数不合法');
		}
		$res = M('city_list')->where(['id'=>$id])->delete();
		if ($res) {
			$this->redirect('city_catgory');
		}

	}

	public function edit_city(){

		$id = I('id');
		if (empty($id) || !is_numeric($id)) {
			$this->error('参数不合法');
		}
		$exists = M('city_list')->where(['id'=>$id])->find();
		if (!$exists) {
			$this->error('分类不存在');
		}
		$this->assign('catgory',$exists);
		$this->display();

	}

	public function edit_city_run(){

		if (!IS_AJAX) {
			$this->error('提交方式不正确');
		}
		$exists = M('city_list')->where(['name'=>$_POST['name']])->where('id!='.$_POST['id'])->find();
		if ($exists) {
			$this->error('分类名已存在');
		}
		$res = M('city_list')->save($_POST);
		if ($res) {
			$this->success('修改成功',U('city_catgory'),1);
		}

	}

	public function add_city(){

		$this->display();

	}

	public function add_city_run(){

		if (!IS_AJAX) {
			$this->error('提交方式不正确');
		}
		$exists = M('city_list')->where(['name'=>$_POST['name']])->find();
		if ($exists) {
			$this->error('国内城市名已存在');
		}
		$res = M('city_list')->add($_POST);
		if ($res) {
			$this->success('添加成功',U('city_catgory'),1);
		}

	}

}